/*
 * @Author: liu wang wang
 * @Date: 2020-07-07 17:32:39
 * @LastEditTime: 2020-08-14 16:48:36
 * @LastEditors: liu wang wang
 * @Description: 业务接口
 * @FilePath: \st-sdk-js\src\index.ts
 */
import St from './core';
import msg from './utils/Msg';
import { StInterface, alertInterface, copyInterface } from '../types';
import { openType, canChange } from './config';
import store from './utils/store';
class STV {
    public st: St | null = null;
    constructor({ theme, app, signUrl }: StInterface) {
        this.st = new St({ theme, app, signUrl });
    }
    protected _open({ zoneid, check = true, versionid, type = 'create', success, close }: { check?: Boolean, zoneid?: number, type?: string, versionid?: number, success?: Function, close?: Function }) {
        (this.st as St).open({
            check,
            type,
            data: {
                zoneid,
                versionid
            },
            success: function (res: any) {
                success && success(res);
            },
            editFun: () => {
                (this.st as St).open({
                    type: openType.EDIT,
                    data: {
                        zoneid,
                        versionid
                    },
                    success: function (res: any) {
                        success && success(res);
                    },
                    close: () => {
                        close && close();
                    }
                });
            },
            setOtherFun: (status: number) => {
                if (status === canChange.yes) {
                    (this.st as St).open({
                        type: openType.CREATE,
                        data: {
                            zoneid,
                            versionid
                        },
                        success: function (res: any) {
                            success && success(res);
                        },
                        close: () => {
                            close && close();
                        }
                    });
                } else {
                    this.concat('由于该工具服已存在开区数据，请联系平台运营调整所属版本。');
                }
            },
            close: () => {
                close && close();
            }
        });
    }
    // 提示弹窗
    public alert({ title = '提示', content, btn, success }: alertInterface): void {
        (this.st as St).alert({ title, content, btn, success });
    }
    // 默认弹窗
    public create({ id, check, success, close }: { id: number, check?: Boolean, success?: Function, close?: Function }): void {
        this._open({ zoneid: id, check, success, close });
    }
    // 提交弹窗
    public submit({ id, success, close }: { id: number, success?: Function, close?: Function }): void {
        this._open({ zoneid: id, type: 'submit', success, close });
    }
    // 编辑弹窗
    public edit({ id, success, close }: { id: number, success?: Function, close?: Function }): void {
        (this.st as St).open({
            type: openType.EDIT,
            data: {
                versionid: id,
            },
            success: function (res: any) {
                success && success(res);
            },
            close: () => {
                close && close();
            }
        });
    }
    // 架区
    public setServer({ zoneid, versionid, success, close }: { zoneid?: number, versionid?: number, success?: Function, close?: Function }): void {
        this._open({ zoneid, versionid, success, close });
    }
    // 查看信息弹窗
    public info({ zoneid, versionid, success, close }: { zoneid?: number, versionid?: number, success?: Function, close?: Function }): void {
        (this.st as St).open({
            type: openType.INFO,
            data: {
                zoneid,
                versionid
            },
            success: function (res: any) {
                success && success(res);
            },
            editFun: () => {
                (this.st as St).open({
                    type: openType.EDIT,
                    data: {
                        zoneid,
                        versionid
                    },
                    success: function (res: any) {
                        success && success(res);
                    },
                    close: () => {
                        close && close();
                    }
                });
            },
            setOtherFun: (status: number) => {
                if (status === canChange.yes) {
                    (this.st as St).open({
                        type: openType.CREATE,
                        data: {
                            zoneid,
                            versionid
                        },
                        success: function (res: any) {
                            success && success(res);
                        },
                        close: () => {
                            close && close();
                        }
                    });
                } else {
                    this.concat('由于该工具服已存在开区数据，请联系平台运营调整所属版本。');
                }
            },
            close: () => {
                close && close();
            }
        });
    }
    // 选择版本弹窗
    public choose({success, gotoCreate, close }:{success: Function, gotoCreate: Function, close?: Function}): void {
        if(!store.getStore('sign')){
            setTimeout(() => {
                this.choose({success, gotoCreate, close });
            }, 500);
        }else{
            (this.st as St).open({
                type: openType.CHOOSE,
                success,
                gotoCreate,
                close
            });
        }
    }
    // 客服弹窗
    public concat(text: string): void {
        (this.st as St).concat(text);
    }
    // 信息轻提示
    public msg(text: string): void {
        msg.msg(text);
    }
    // 复制工具服
    public copy({ oldZoneID, newZoneID, success }: copyInterface): void {
        (this.st as St).copy({
            data:{
                oldZoneID,
                newZoneID,
            },
            success: function (res: any) {
                success && success(res);
            }
        })
    }
    // 关闭弹窗
    public close(): void {
        (this.st as St).close();
    }
}
const version = 'v1.0.0';
export { STV as init , version}

